﻿using DnD.Common;
using DnD.Model;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;

namespace DnD.ViewModels
{
    class CardListViewModel : INotifyPropertyChanged
    {

        private PhoneApplicationPage _view;

        public ObservableCollection<Card> Cards { get; set; }
        public ObservableCollection<Deck> Decks { get; set; }
        public DelegateCommand PlayCommand { get; set; }

        private Deck _selectedDeck;
        private Card _selectedCard;

        public Deck SelectedDeck
        {
            get { return _selectedDeck; }
            set
            {
                _selectedDeck = value;
                OnPropertyChanged();
                showDeck(value);
            }
        }

        public Card SelectedCard
        {
            get { return _selectedCard; }
            set
            {
                _selectedCard = value;
                OnPropertyChanged();
                showCard(value);
            }
        }


        public CardListViewModel(PhoneApplicationPage view)
        {

            using (var context = new DnDDataContext(DnDDataContext.DbConnectionString))
            {
                Cards = new ObservableCollection<Card>(context.GetTable<GameItem>().OfType<Card>());
                Decks = new ObservableCollection<Deck>(context.GetTable<GameItem>().OfType<Deck>());
            }

            _view = view;

            PlayCommand = new DelegateCommand(play, name => true);

        }


        

        public void showDeck(Object deck)
        {
            _view.NavigationService.Navigate(new Uri("/DeckPage.xaml?deckID=" + (deck as Deck).Id, UriKind.Relative));
        }

        public void showCard(Object card)
        {
            Card pressedCard = card as Card;

            
            _view.NavigationService.Navigate(new Uri("/CardPage.xaml?cardID=" + (pressedCard.Id).ToString(), UriKind.Relative));
           
        }

        public void play(object name)
        {

            _view.NavigationService.Navigate(new Uri("/GameSetupPage.xaml", UriKind.Relative));
        }

        public void handleMenuRequest(String menuItemName)
        {
            if(menuItemName == "profile")
            {
                _view.NavigationService.Navigate(new Uri("/UserPage.xaml", UriKind.Relative));
            }
            else if (menuItemName == "browse")
            {
                _view.NavigationService.Navigate(new Uri("/BrowsePage.xaml", UriKind.Relative));
            }
        }

        public ImageBrush BackgroundBrush 
        {
            get
            {
                if ((Visibility)App.Current.Resources["PhoneDarkThemeVisibility"]
                  == Visibility.Visible)
                {

                    
                    ImageBrush imgBrush = new ImageBrush(); 
                    imgBrush.ImageSource = new BitmapImage(new Uri("/Assets/Pictures/dragon3.jpg", UriKind.Relative)); 
                    return imgBrush;

                }
                else
                {
                    ImageBrush imgBrush = new ImageBrush(); 
                    imgBrush.ImageSource = new BitmapImage(new Uri("/Assets/Pictures/dragon3_negative.jpg", UriKind.Relative)); 
                    return imgBrush;

                }
            }
            private set { }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

    }


}
